
/**
 * @description: 初始化智能配置页面
 * @author: elvis
 */

import { useState, useEffect } from 'react';
import EditInitChannel from '../component/EditInitChannel';
import CommonLoading from '@/components/CommonLoading';
import { init } from '../../service';


export default ({ query }) => {

	// 智能初始化渠道
	const [initChannel, setInitChannel] = useState({});

	//初始化渠道设置弹框
	const [visible, setVisible] = useState(false);

	const [loading, setLoading] = useState(false);

	//初始化请求配置信息
	useEffect(() => {
		getInitChannel();
	}, [ query.deliverType ])

	// 获取初始化智能配置
	const getInitChannel = async () => {
		setLoading(true);
		const { data, success } = await init({
			type: query.deliverType
		});
		if (success) {
			setInitChannel(data);
		}
		setLoading(false);
	};

	//点击修改初始化发货渠道
	const EditChannel = () => {
		setVisible(true);
	};

	//修改初始化渠道弹框
	const EditInitChannelProps = {
		type: query.deliverType,
		initChannel,
		visible,
		handleOk() {
			getInitChannel();
			setVisible(false);
		},
		handleCancel() {
			setVisible(false);
		},
	};

	return (
		<aside className="common-color4" style={{ marginBottom: '12px' }}>
			初始化智能配置：{initChannel.channelName}
			<span onClick={EditChannel} style={{ marginLeft: 16 }} className="common-color2">
				修改
			</span>
			<EditInitChannel {...EditInitChannelProps} />
			<CommonLoading loading={loading} />
		</aside>
	)
}